//
//  MYNewFeatureCollectionViewController.m
//  奇闻
//
//  Created by 似水灵修 on 14-11-24.
//  Copyright (c) 2014年 MingYueGuCheng. All rights reserved.
//

#import "MYNewFeatureCollectionViewController.h"
#import "MYNewfeatureCell.h"
//图片数量
#define MYImageCount 4
@interface MYNewFeatureCollectionViewController ()
@property (nonatomic, weak) UIPageControl *pageControl;
@end

@implementation MYNewFeatureCollectionViewController

static NSString * const reuseIdentifier = @"Cell";
- (instancetype)init
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = MYScreenBounds.size;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;//水平滚动
    layout.minimumLineSpacing = 0;
    return [self initWithCollectionViewLayout:layout];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //注册cell
    //self.view != self.collentionView
    self.collectionView.backgroundColor = MYRandomColor;
    
    [self.collectionView registerClass:[MYNewfeatureCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    self.collectionView.pagingEnabled = YES;
    self.collectionView.bounces = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    // 添加pageControll
    [self setupPageControl];
    
}
- (void)setupPageControl
{
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.numberOfPages = MYImageCount;
    pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    pageControl.pageIndicatorTintColor = [UIColor blackColor];
    pageControl.center = CGPointMake(self.view.width * 0.5, self.view.height * 0.95);
    [self.view addSubview:pageControl];
    _pageControl = pageControl;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return MYImageCount;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MYNewfeatureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    //拼接图片名
    NSString *imageName = [NSString stringWithFormat:@"new_feature_%zi", indexPath.item + 1];
    cell.image = [UIImage imageNamed:imageName];
    [cell setIndexPath:indexPath itemCount:MYImageCount];
    return cell;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger page = scrollView.contentOffset.x / self.view.width + 0.5;
    self.pageControl.currentPage = page;
}
@end
